package com.tfswue.group.controller;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.tfswue.group.entity.User;
import com.tfswue.group.service.UserService;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.responses.ApiResponse;
import io.swagger.v3.oas.annotations.tags.Tag;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.cache.annotation.CacheEvict;
import org.springframework.cache.annotation.CachePut;
import org.springframework.cache.annotation.Cacheable;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@Slf4j
@RestController
@RequestMapping("/api/users")
@Tag(name="用户接口", description = "符合RESTful风格的用户管理接口")
public class UserController {
    @Autowired
    private UserService userService;

    public UserController() {
        log.info("UserController 已初始化");
    }

    @Operation(summary = "查询所有用户", description = "获取系统中所有用户的列表")
    @ApiResponse(responseCode = "200", description = "成功获取用户列表")
    @GetMapping
    public List<User> getAllUsers(){
        return userService.list();
    }

    @Operation(summary = "查询单个用户", description = "根据ID查询用户")
    @ApiResponse(responseCode = "200", description = "成功获取用户信息")
    @Cacheable(value = "dataCache", key="#id")
    @GetMapping("/{id}")
    public User getUserById(@PathVariable Integer id) {
        log.info("查询用户ID: {}", id);
        return userService.getById(id);
    }

    @Operation(summary = "分页查询用户", description = "分页获取用户列表")
    @ApiResponse(responseCode = "200", description = "成功分页获取用户")
    @GetMapping("/querypage/{page}/{size}")
    public List<User> queryPage(@PathVariable("page") Integer page,
                                @PathVariable("size") Integer size) {
        Page<User> userPage = new Page<>(page, size);
        return userService.page(userPage).getRecords();
    }

    @Operation(summary = "条件分页查询用户", description = "根据条件分页查询用户")
    @ApiResponse(responseCode = "200", description = "成功条件查询用户")
    @GetMapping("/querylist/{page}/{size}")
    public List<User> queryPageByCondition(@PathVariable("page") Integer page,
                                           @PathVariable("size") Integer size,
                                           @RequestBody(required = false) User user) {
        QueryWrapper<User> queryWrapper = new QueryWrapper<>();
        if (user != null) {
            if (user.getUsername() != null) {
                queryWrapper.like("username", user.getUsername());
            }
            if (user.getEmail() != null) {
                queryWrapper.eq("email", user.getEmail());
            }
        }
        Page<User> userPage = new Page<>(page, size);
        return userService.page(userPage, queryWrapper).getRecords();
    }

    @Operation(summary = "添加用户", description = "添加新用户")
    @ApiResponse(responseCode = "200", description = "成功添加用户")
    @PostMapping("/api/users")
    public boolean addUser(@RequestBody User user) {
        return userService.save(user);
    }

    @Operation(summary = "更新用户", description = "更新用户信息")
    @ApiResponse(responseCode = "200", description = "成功更新用户")
    @CachePut(value = "dataCache", key = "#user.id")
    @PutMapping
    public boolean updateUser(@RequestBody User user) {
        return userService.updateById(user);
    }

    @Operation(summary = "删除用户", description = "根据ID删除用户")
    @ApiResponse(responseCode = "200", description = "成功删除用户")
    @CacheEvict(value = "dataCache", key = "#id")
    @DeleteMapping("/{id}")
    public boolean deleteUser(@PathVariable Integer id) {
        return userService.removeById(id);
    }

}
